var wind = window;
(function(window) {
    var passwordFirstBox = $(".password_first");
    var passwordSecondBox = $(".password_second");
    var passwordThirdBox = $(".password_third");

    var passwordLastCtl = $(".password_last");
	var checkAccoutCtl = $(".password_next");

	var verifytypeObj = $("#verifytype");
	var verifytype = verifytypeObj.val();
	var telverifyObj = $("#telverify");
	var verifyObj = $("#verify");

	var verifyvalue, new_password, pwdconfirm;


	var newPasswordObj = $("#password");
	var pwdconfirmObj = $("#pwdconfirm");

	var telverifySendBtn = $("#telverifySendBtn");

	checkAccoutCtl.click(function(e){
        e.preventDefault();
		switch (verifytype) {
			case 'phone':
				verifyvalue = $("#phone").val();
				break;
			case 'email':
				verifyvalue = $("#email").val();
				break;
		}
        var telverify = telverifyObj.val();
		var verify = verifyObj.val();
        if (verify == '') {
            alert("图片验证码不能为空");
            verifyObj.focus();
            return false;
        }else if (telverify == '') {
            alert("验证码不能为空");
            telverifyObj.focus();
            return false;
        } else {
			var p={};
            p['verifyvalue']=verifyvalue;
        	p['verifytype']=verifytype;
        	p['telverify']=telverify;
        	p['verify']=verify;
			p['checkverify']=$("#checkverify").val();
            $.ajax({
	            type: "post",
	            dataType: "json",
	            url: checkAccoutUrl,
	            data: p,
	            success: function (data) {
	                console.log(data);
	                if(data.code==200){
	                	passwordFirstBox.hide();
						passwordSecondBox.show();
						passwordThirdBox.hide();
	                } else {
						if($("#checkverify").val()=='1'){
							var verifyimg = $(".verifyimg").attr("src");
							if (verifyimg.indexOf('?') > 0) {
								$(".verifyimg").attr("src", verifyimg + '&random=' + Math.random());
							} else {
								$(".verifyimg").attr("src", verifyimg.replace(/\?.*$/, '') + '?' + Math.random());
							}
							$("#verify").focus();
						}
	                	alert(data.msg);
	                }
	            }
	        });
        }
    })

	passwordLastCtl.click(function(e){
        e.preventDefault();
        switch (verifytype) {
			case 'phone':
				verifyvalue = $("#phone").val();
				break;
			case 'email':
				verifyvalue = $("#email").val();
				break;
		}
		new_password = newPasswordObj.val();
    	pwdconfirm = pwdconfirmObj.val();
		if (new_password == '') {
			alert("密码不能为空");
			newPasswordObj.focus();
			return false;
		} else if (pwdconfirm == '') {
			alert("确认密码不能为空");
			pwdconfirmObj.focus();
			return false;
		} else if (pwdconfirm != new_password) {
			alert("两次密码不一样");
			pwdconfirmObj.focus();
			return false;
		} else {
			var p={};
			p['new_password']=new_password;
            p['username']=verifyvalue;
			$.post(forgotPasswordUrl,p,function(data){
				if(data.code==200){
					passwordFirstBox.hide();
					passwordSecondBox.hide();
					passwordThirdBox.show();
				}else{
					alert(data.msg)
				}
			})
		} 
    })

	telverifySendBtn.click(function(e){
        switch (verifytype) {
			case 'phone':
				verifyvalue = $("#phone").val();
				break;
			case 'email':
				verifyvalue = $("#email").val();
				break;
		}
        SendVerify.sendMessage(telverifySendBtn,verifyvalue,verifytype,tplname);
	})

})(window);